"""Trend analysis schemas."""

from __future__ import annotations

from datetime import datetime
from typing import Literal

from pydantic import Field

from app.schemas.base import ORMBase


class TimeSeriesPoint(ORMBase):
    """Single point in time-series data."""

    timestamp: str = Field(..., description="Time bucket (formatted datetime)")
    count: int = Field(..., description="Number of data points in bucket")
    avg_value: float = Field(..., description="Average value in bucket")
    min_value: float = Field(..., description="Minimum value in bucket")
    max_value: float = Field(..., description="Maximum value in bucket")


class MovingAveragePoint(ORMBase):
    """Single point with moving average."""

    timestamp: datetime
    value: float
    moving_average: float | None = Field(None, description="Moving average value (null until window is full)")


class TrendAnalysis(ORMBase):
    """Trend analysis result."""

    device_id: int
    metric_name: str
    time_window_hours: int
    trend_direction: Literal["increasing", "decreasing", "stable"] = Field(
        ..., description="Overall trend direction"
    )
    change_percent: float = Field(..., description="Percent change from first to last value")
    first_value: float | None = Field(None, description="First value in time window")
    last_value: float | None = Field(None, description="Last value in time window")
    avg_value: float | None = Field(None, description="Average value in time window")
    data_points: int = Field(..., description="Number of data points analyzed")
    threshold_percent: float = Field(..., description="Threshold used for trend detection")


class RateOfChangePoint(ORMBase):
    """Rate of change data point."""

    timestamp: datetime
    value: float
    rate_of_change: float = Field(..., description="Rate of change per interval")
    time_diff_seconds: float = Field(..., description="Time difference from previous point")
